package com.zmj.workorder.controller;


import com.zmj.project.bean.Project;
import com.zmj.utils.Response;
import com.zmj.workorder.bean.TableData;
import com.zmj.workorder.bean.WorkOrder;
import com.zmj.workorder.service.WorkOrderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/workOrder/")
public class WorkOrderController {
    @Resource
    private WorkOrderService workOrderService;

    @RequestMapping("savaWorkOrder")
    @ResponseBody
    public Response savaWorkOrder(WorkOrder workOrder, HttpSession session){
        Project project=(Project) session.getAttribute("project");
        workOrder.setProject_id(project.getId());

        boolean flg=workOrderService.savaWorkOrder(workOrder);
        Response response=new Response();
        if (flg){
            response.setCode(1);
            response.setMessage("添加成功");

        }else {
            response.setCode(-1);
            response.setMessage("添加失败");
        }
        return  response;
    }
    @RequestMapping("toList")
    public  String toList(HttpServletRequest request){
         List<WorkOrder> workOrders=workOrderService.getWorkOrder();
         request.setAttribute("workOrders", workOrders);
        return  "list";
    }

    @RequestMapping("findAllWorkToTable")
    @ResponseBody
    /**
     * 参数：
     2
     * offset: 10
     * limit: 10
     * 3
     * offset: 20  startIndex
     * limit: 10   pageSize
     *
     * 返回值
     {total:xxx,rows:[{},{},{}]}
     */
    public TableData findWork(int  offset, int limit, String order , String search){
        System.out.println(offset+"-----"+limit);


        TableData tableData = workOrderService.findByPage(offset,limit, order , search);
        return  tableData;

    }

}
